<template>
	<view id="content">
		<view class="gift_item" v-for="(item,index) in gift_list" :key="item.id">
			<view style="display: flex;justify-content: space-between;">
				名称：{{item.name || '暂未填写'}}
				<span v-if="item.num > 0" class="sel_ok">✔</span>
			</view>
			<!-- <view style="margin: 16rpx 0 0 0;color: #888;">ID：{{item.id}}</view> -->

			<uni-number-box class="number_box" v-model="item.num" :width="44" :max="99"></uni-number-box>

			<view style="height: 1rpx;width: 100%;background-color: #ddd;margin: 16rpx 0 0 0;"></view>

		</view>

		<!-- 占位布局 -->
		<view style="width: 100%;height: 120rpx;"></view>

		<view class="op_btn_div">
			<view id="btn_ok" @click.stop="onOk">选择完毕</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				gift_list: [],
			}
		},

		mounted() {
			this.getGiftList()
		},

		methods: {

			// 点击选择完毕
			onOk() {
				let that = this
				let arr = that.analysisGift()
				uni.$emit('onSelGift', { msg: arr })
				uni.navigateBack()
			},

			// 解析选择的赠品
			analysisGift() {
				let arr = []
				this.gift_list.map((item, index) => {
					if (item.num > 0) {
						arr.push({ gift_id: item.id, name: item.name, num: item.num })
					}
				})
				return arr
			},

			// 接口:安装型号列表
			async getGiftList() {
				this.gift_list = []
				let that = this
				uni.showLoading({ title: '处理中', mask: true, })
				let res = await this.$getGift()
				// console.log('$getGift.res =', res);
				if (res.code == 1) {
					this.gift_list = res.data.map((item, index) => {
						item.num = 0
						return item
					})
				} else {
					this.$toast(res.msg)
				}
			},
		}
	}
</script>

<style scoped>
	#content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.gift_item {
		display: flex;
		flex-direction: column;
		margin: 20rpx;
		font-size: 28rpx;
	}

	.number_box {
		margin: 24rpx 0 0 0;
	}

	.sel_ok {
		font-size: 32rpx;
		font-weight: bold;
		color: orangered;
	}

	/* 操作按钮容器 */
	.op_btn_div {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		width: calc(100%);
		margin: 40rpx 0rpx 0 0rpx;
		background-color: white;
	}

	#btn_ok {
		flex-grow: 1;
		/* width: 300rpx; */
		height: 76rpx;
		line-height: 76rpx;
		text-align: center;
		background-color: deepskyblue;
		color: white;
		margin: 20rpx 40rpx 20rpx 40rpx;
		border-radius: 10rpx;
	}
</style>